package main

import (
	"fmt"
	"gitee.com/sdzxq/api_server/conf"
	"gitee.com/sdzxq/api_server/middleware"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)


func routerBundle() http.Handler{
	// make A main Router
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Welcome to our API SERVER 1.0, enjoy our service please",
		})
	})

	// prepare for API V1
	v1 := r.Group("/api/v1")
	{
		v1.GET("/", sayHello)
		v1.POST("/", sayNo)
		v1.GET("/db_status", dbStatus)

		// AUTHENTICATE operation group
		auth := v1.Group("/auth")
		{
			auth.POST("/validator", getToken) // if username & pwd are both validated then return a token.
		}

		// USER operation group with AUTH middleware.
		user := v1.Group("/user")
		{
			// Single user operations.
			user.Use(middleware.SingleTokenVerify())
			user.GET("/:id", getOneUser)
			user.POST("/",createOneUser)
			user.PUT("/:id")
			user.DELETE("/:id")
			user.GET("/:id/user_info")
			user.POST("/:id/user_info")
			user.PUT("/:id/user_info")
		}

		// USERS operation group with AUTH middleware
		users := v1.Group("/users")
		{
			// multi users operations
			users.POST("/list")
			users.GET("/list")
			users.DELETE("/list")
		}

		// NEWS operation group without AUTH middleware.

		{

		}

	}
	return r
}

func main() {
	r := routerBundle()

	s := &http.Server{
		Addr:conf.Port,
		Handler: r,
		ReadTimeout: conf.ReadTimeout,
		WriteTimeout: conf.WriteTimeout,
	}

	if err := s.ListenAndServe(); err != nil {
		fmt.Println(err)
		log.Fatal("Server has a 500 error, please contact our Admin! ")
	}
}